class gpage3 extends BaseScreen {
    public constructor() {
        super();
        this.skinName = "resource/eui_skins/page3.exml";      
    }

	private xzt:Array<Object>;
	private pdt:Array<Object>;
	
	private subject_Arr:Array<subjectTitle>;
	private img_p3_btn:eui.Image;
	private img_next:eui.Image;

	private game_time:number = 30000;
	private gameing_time:number = 0;
	//1 等待  2 进行中 3 结束 
	public game_state:number = 0;

	private lab_time:eui.Label;
	private scroller_bg:eui.Scroller;
	private img_earth:eui.Image;
	private g_page4:gpage4;
	public group_loading:screenLoad;
	private group_result:eui.Group;
	private lab_result:eui.Label;
	private img_p2_next_btn:eui.Image;
	protected childrenCreated(): void {
        super.childrenCreated();
        this.init_data();
		this.init_view();
		this.update_sate(2);
    }

	public init_view(){
		this.subject_Arr = new Array();
		this.subject_Arr.push(this['subject_1']);
		this.subject_Arr.push(this['subject_2']);
		this.subject_Arr.push(this['subject_3']);
		
		var rand_id = Math.ceil(Math.random()*(this.xzt.length-1));
		this.subject_Arr[0].set_info(this.xzt[rand_id],0,this);
		this.xzt.splice(rand_id,1);

		console.log('rand_id='+rand_id);

		rand_id = Math.ceil(Math.random()*(this.xzt.length-1));
		this.subject_Arr[1].set_info(this.xzt[rand_id],1,this);
		this.xzt.splice(rand_id,1);

		console.log('rand_id1='+rand_id);

		rand_id = Math.ceil(Math.random()*(this.pdt.length-1));
		this.subject_Arr[2].set_info(this.pdt[rand_id],2,this);

		console.log('rand_id2='+rand_id);

		this.group_loading = new screenLoad();
		this.addChild(this.group_loading);
		this.group_loading.close();
		this.group_result.visible = false;

		this.img_p3_btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.submit_hander,this);
		this.img_next.addEventListener(egret.TouchEvent.TOUCH_TAP,this.next_hander,this);		
		this.addEventListener(egret.Event.ENTER_FRAME, this.OnEnterFrame, this);
		this.img_p2_next_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.close_tips, this);
		
		
	}

	public init_data(){
		this.pdt = RES.getRes('pd_json').tm;
		this.xzt = RES.getRes('xz_json').tm;
		
		// RES.get

		// console.log(this.pdt);
		// console.log(this.xzt);

		for(var i = 0;i < this.xzt.length;i++){
			this.xzt[i]['tp'] = 1;
			
		}

		for(var i = 0;i < this.pdt.length;i++){
			this.pdt[i]['tp'] = 2;
		}		
	}

	public close_tips(){
		this.group_result.visible = false;
	}

	public submit_hander(e:egret.TouchEvent){
		for(var i =0;i<this.subject_Arr.length;i++){
			this.subject_Arr[i].show_result();
		}

		this.img_next.visible = true;
		this.img_p3_btn.visible = false;

		this.group_result.visible = true;
		this.group_result.alpha = 0;
		this.lab_result.text = utils.CoreData.instance.cur_Dtsource.toString();
		egret.Tween.get(this.group_result).to({'alpha':1},500).call(function(){
			utils.ServerManager.instance.requestLogin(utils.CoreData.instance.cur_source);
		},this);

		
	}

	public next_hander(e:egret.TouchEvent){
		//next_hander
		this.changeScreen();
	}


	//转换场景
    private changeScreen(): void {
        this.clear();
        // utils.ScreenManager.instance.changeToShowScreen(4);
		this.commitSuccessHandle();
    }

	public clear(){
		this.img_p3_btn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.submit_hander,this);
		this.img_next.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.next_hander,this);		
		this.removeEventListener(egret.Event.ENTER_FRAME, this.OnEnterFrame, this);
	}

	private OnEnterFrame(e: egret.Event) {
		if(this.game_state == 2){
			var cur_time:number = egret.getTimer();
			var t_time:number = cur_time - this.gameing_time;
			
			if(t_time > this.game_time){
				this.update_sate(3);
				this.lab_time.text = "00:00’s";
			}
			
			var cur_time = (Math.ceil((this.game_time - t_time)/1000));
			if(cur_time>10){
				this.lab_time.text = '00:'+cur_time+"";
			}
			else{
				this.lab_time.text = '00:0'+cur_time+"";
			}
		}

		this.img_earth.rotation +=1;
		
	}

	private update_sate(c_state:number){
		this.game_state = c_state;
		if(c_state == 2){
			this.img_p3_btn.visible = false;
			this.gameing_time = egret.getTimer();
		}
		else if(c_state == 3){
			this.img_p3_btn.visible = true;
		}
	}

	public is_select_all(){
		var result_count:number = 0;
		for(var i =0;i<this.subject_Arr.length;i++){
			if(this.subject_Arr[i].is_answer){
				result_count+=1;
			}
		}
		if(result_count >= 3){
			this.update_sate(3);
		}
	}

	public resize(): void {
        super.resize();
		this.scroller_bg.height = this.height;

		this.group_result.y = (this.height - this.group_result.height)*0.5;
        // this.group_time.y = this.height - 200;
    }


	private commitSuccessHandle():void{
        console.log("提交信息成功!!");
        this.g_page4=new gpage4()
		this.g_page4.visible=false;
		this.addChild(this.g_page4);
		// this.g_page4.x = this.g_page4.y = 0;
        this.group_loading.show();
		setTimeout(this.createResultPhoto.bind(this),500);
    }
    private createResultPhoto():void
	{
		var renderTexture:egret.RenderTexture = new egret.RenderTexture();
        renderTexture.drawToTexture(this.g_page4);
		this.removeChild(this.g_page4);
		this.group_loading.close();
		console.log('this.g_page4.width='+this.g_page4.width+'--'+this.g_page4.width*0.8);
		console.log('this.g_page4.height='+this.g_page4.height+'--'+this.g_page4.height*0.8);
		
        var imageBase64:string = renderTexture.toDataURL("image/jpg",new egret.Rectangle(0,0,640,1386));
		// var imageBase64:string = renderTexture.toDataURL("image/jpg",new egret.Rectangle(0,0,512,1108));
        this.showImgToHtml(imageBase64);
	}

    public showImgToHtml(htmlCodeUrl:string){
		var gameDiv = document.getElementById("btn_div");
		let cutImg = document.createElement("img");
		cutImg.src = htmlCodeUrl;
		cutImg.style.position = "absolute";
        cutImg.style.top = "0";
        cutImg.style.left = "0";
        cutImg.style.width=document.body.clientWidth+"px";
        // cutImg.style.height=document.body.clientHeight+"px";
		gameDiv.appendChild(cutImg);
	}

	
}